﻿using System;
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.Tilemaps;

namespace ns
{
    public class FSMBase : MonoBehaviour
    {
        #region 脚本声明周期
        private void Start()
        {
            ConfigFSM();
            InitComponent();
            InitDefaultState();
        }
        public void Update()
        {
            Test_currentStateID = currentStateID;
            currentState.Reason(this);
            currentState.ActionState(this);
            Search();
        }

        private void Search()
        {
            location = tilemap.WorldToCell(transform.position);
            WaterTile = tilemap.GetTile(location);
            for (int i = 0; i < 3; i++)
            {
                FireRingTile[i] = tilemap.GetTile(location + new Vector3Int(1, 2 + i, 0));
            }
            magic = magicmap.GetTile(location + new Vector3Int(1, 0, 0));
            try
                {
                    airsmoke = GameObject.FindObjectOfType<Smoke>().transform;
                }
            catch (Exception)
                {
                    
                }
        }
        #endregion

        #region 状态机自身成员
        private FSMState currentState;
        private FSMState defaultState;
        private List<FSMState> states;
        [Tooltip("默认状态编号")]
        public FSMStateID defaultStateID;
        public FSMStateID currentStateID { get; private set; }
        [SerializeField]
        private FSMStateID Test_currentStateID;
        public string fileName;
        private void InitDefaultState()
        {
            defaultState = states.Find(s => s.StateID == defaultStateID);
            currentState = defaultState;
            currentStateID = defaultStateID;
            currentState.EnterState(this);
        }
        private void ConfigFSM()
        {
            states = new List<FSMState>();
            var map = AIConfigurationReaderFactory.GetMap(fileName);
            foreach (var state in map) 
            {
                Type type = Type.GetType("ns."+ state.Key + "State");
                FSMState stateobj=Activator.CreateInstance(type) as FSMState; 
                states.Add(stateobj);
                foreach (var list in state.Value)
                {
                    FSMTriggerID triggerID= (FSMTriggerID)Enum.Parse(typeof(FSMTriggerID),list.Key);
                    FSMStateID stateID = (FSMStateID)Enum.Parse(typeof(FSMStateID),list.Value);
                    stateobj.AddMap(triggerID, stateID);
                }
            }
        }
        public void ChangeActiveState(FSMStateID stateID)
        {
            currentState.ExitState(this);
            currentState=stateID==FSMStateID.Default?defaultState:states.Find(s=>s.StateID==stateID);
            currentStateID = stateID;
            currentState.EnterState(this);
        }
        #endregion

        #region 为状态和条件提供的外界成员
        public Animator animator;
        public CharacterAnimationParam chParams;
        public Tilemap tilemap;
        public Vector3Int location;
        public TileBase WaterTile;
        public TileBase[] FireRingTile;
        public Dictionary<string, TileBase> specialTiles;
        public Dictionary<string, GameObject> specialTiles2;
        public float speed;
        public bool isgo;
        public bool outfire;
        public Transform airsmoke;
        public Vector3Int testposition;
        public TileBase magic;
        public Vector3Int magicposition;
        public Vector3Int magicposition1;
        public bool magicout;
        public bool magicover;
        public Tilemap magicmap;
        public TileBase w1;
        public TileBase w2;
        public TileBase f1;
        public TileBase mg;
        public GameObject sm;
        public void InitComponent()
        {
            specialTiles = new Dictionary<string, TileBase>();
            specialTiles["water1"] = w1;
            specialTiles["water2"] = w2;
            specialTiles["fire"] = f1;
            specialTiles["magic"] = mg;
            specialTiles2 = new Dictionary<string, GameObject>();
            specialTiles2["smoke"] = sm;
            magicmap = GameObject.Find("magic").GetComponent<Tilemap>();
            magicover = false;
            magicout=false;
            airsmoke = null;
            outfire =false;
            isgo = false;
            animator = GetComponentInChildren<Animator>();
            location = tilemap.WorldToCell(transform.position);
            WaterTile = tilemap.GetTile(location);
            magic = tilemap.GetTile(location + Vector3Int.right);
            FireRingTile = new TileBase[3];
            for (int i = 0; i < 3; i++)
            {
                FireRingTile[i] = tilemap.GetTile(location + new Vector3Int(1, 2 + i, 0));
            }
        }
        #endregion
    }
}